package com.spring.task1;

import com.spring.task1.model.Address;
import com.spring.task1.model.Student;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.boot.ApplicationArguments;
import org.springframework.boot.ApplicationRunner;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.ApplicationContext;
import org.springframework.context.annotation.Bean;
import org.springframework.context.support.ClassPathXmlApplicationContext;
/**
 * 总结三种方式：
 * 1、基于xml的方式
 * 2、基于注解方式
 * 3、基于Java配置类方式
 */
@SpringBootApplication
@Slf4j
public class SpringTask1Application implements ApplicationRunner {

    /**
     * 注解导入
     */
    @Autowired
    @Qualifier(value = "address2")
    private Address address2;

    @Autowired
    @Qualifier(value = "student1")
    private Student student1;


    public static void main(String[] args) {
        SpringApplication springApplication = new SpringApplication(SpringTask1Application.class);
        springApplication.setAllowCircularReferences(Boolean.TRUE);
        springApplication.run(args);
    }

    @Override
    public void run(ApplicationArguments args) throws Exception {

        ApplicationContext applicationContext = new ClassPathXmlApplicationContext(
                new String[] {"applicationContext.xml"});

        //xml 的字段元素依赖注入
        Address address1 = applicationContext.getBean("address1", Address.class);
        log.info("xml 的字段元素依赖注入， address1={}",address1);

        //xml 的构造器注入
        Address address3 = applicationContext.getBean("address3", Address.class);
        log.info("xml 的构造器注入， address3={}",address3);

        //configuration 基于Java配置类方式 注解bean
        log.info("configuration 基于Java配置类方式装载， address2={}",address2);

        //直接 实体类中注解托管 bean方式
        log.info("实体类中注解配置方式装载， student1={}",student1);

    }



}
